<?

/*
 * ************ 分页类 *************
 * Version 2.0
 * Author:Dicky Pan
 * Release Date:2009-1-14
 * E-Mail:3606302@163.com
 *
 * 继承DBO数据库操作类的分页类
 */

class SplitPage extends DBO {

    public $totalPage; //总分页数
    public $totalResult; //总记录数
    public $pageSize; //每页显示数量
    public $currentPage; //当前页
    private $currentUrl; //当前URL
    public $statisticsText; //统计文字
    public $isShowStatistics; //是否显示统计文字
    public $pageGroupNum; //页码每个分组的数量
    public $isShowPageGroup; //是否显示页码分组
    public $isShowPageJump; //是否显示页码分组

    //构造函数

    function __construct() {
	//执行DBO的构造函数
	parent::__construct ();

	//设置默认值
	$this->pageSize = 10;
	$this->pageGroupNum = 5;
	$this->currentPage = 1;
	$this->currentUrl = $this->getCurrentUrl();
	$this->isShowStatistics = true;
	$this->isShowPageGroup = true;
	$this->isShowPageJump = false;
	//$this->statisticsText = '' . $this->currentPage . '';
	$this->statisticsText = '';
    }

    //获取总分页数
    private function getTotalPage() {
	return ceil($this->resultNum / $this->pageSize);
    }

    //覆盖dbOperate的query方法 加入分页需要使用的SQL语句
    public function query($sql) {
	if (empty($sql)) {
	    $this->DisplayError('The lack of query!');
	    return null;
	} else {
	    $queryOperatet = strtolower(strtok($sql, ' '));
	    if ($queryOperatet == 'select') {
		parent::query($sql);
		$this->totalPage = $this->getTotalPage();
		$this->totalResult = $this->resultNum;
		if ($this->currentPage < 1) {
		    $this->currentPage = 1;
		}
		$start_result = $this->pageSize * ($this->currentPage - 1);
		$sql .= " limit $start_result,$this->pageSize";
		$rs = parent::query($sql);
		return $rs;
	    } else {
		return null;
	    }
	}
    }

    //翻页HTML代码
    public function getHtmlCode() {
	//$html = '<div id="SplitPageBox">';
	//if($this->isShowStatistics){
	//$html .= '<div id="SplitPage_Statistics">' . $this->getStatisticsText () . '</div>';
//		}
//	if ($this->isShowPageJump) {
//	    $html .= '<div id="SplitPage_Goto">
//							<table border="0" cellspacing="0" cellpadding="0">
//								<tr>
//									<td>到</td>
//									<td><input id="page" name="page" type="text" class="SplitPageInputBox" /></td>
//									<td>页</td>
//									<td><input type="button" value="GO" maxlength="3" class="SplitPageBotton" onclick="self.location=\'' . $this->currentUrl . '&page=\'+document.getElementById(\'page\').value" /></td>
//								</tr>
//							</table>
//					</div>';
//	}
	$html .= $this->getPrevious();
	if ($this->isShowPageGroup) {
	    $html .= $this->getPageList();
	}
	$html .= $this->getNext();
	return $html;
    }

    //根据统计文本范本生成统计本文
    private function getStatisticsText() {
	$text = str_replace('{ResultNum}', $this->totalResult, $this->statisticsText);
	$text = str_replace('{TotalPage}', $this->totalPage, $text);
	$text = str_replace('{PageSize}', $this->pageSize, $text);
	return $text;
    }

    //设置前一页按钮
    private function getPrevious() {
	if ($this->currentPage == 1) {
	    return '<li class="long">Previous</li>';
	} else {
	    return '<li class="long"><a href="' . $this->currentUrl . '&page=' . ($this->currentPage - 1) . '" page="'.($this->currentPage - 1).'">Previous</a></li>';
	}
    }

    //设置后一页按钮
    private function getNext() {
	if ($this->currentPage == $this->totalPage) {
	    return '<li class="long">Next</li>';
	} else {
	    return '<li class="long"><a href="' . $this->currentUrl . '&page=' . ($this->currentPage + 1) . '" page="'.($this->currentPage + 1).'" >Next</a></li>';
	}
    }

    //获取页码列表
    private function getPageList() {
	//将页码分段，并获取当前页所在的页码段
	$tmp = intval(($this->pageGroupNum - 1) / 2);

	$start_page = $this->currentPage - $tmp;
	if ($start_page <= 0) {
	    $start_page = 1;
	}

	$end_page = $start_page + $this->pageGroupNum;

	if ($this->currentPage > $this->totalPage - $this->pageGroupNum + 1) {
	    $start_page = $this->totalPage - $this->pageGroupNum + 1;
	    $end_page = $this->totalPage + 1;
	    if ($start_page <= 0) {
		$start_page = 1;
	    }
	}

	for ($i = $start_page; $i < $end_page; $i++) {
	    if ($i == $this->currentPage) {
		$list_html_code .= '<li class="Selected">' . $i . '</li>';
	    } else {
		$list_html_code .= '<li><a href="' . $this->currentUrl . '&page=' . $i . '" page="'.$i.'">' . $i . '</a></li>';
	    }
	}
	return $list_html_code;
    }

    //获取当前页面的完整URL
    private function getCurrentUrl() {
	$url_query = $_SERVER ['QUERY_STRING']; //获取当前页面GET参数
	$url_query = preg_replace("/page=\\d*|&page=\\d*/", "", $url_query); //过滤当前URL中的Page
	$url_query = trim($url_query, '&');
	return $_SERVER ['PHP_SELF'] . "?" . $url_query;
    }

}

?>
